package com.keldian.mobilesafe.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.keldian.mobilesafe.R;
import com.keldian.mobilesafe.utils.SmsUtils;

public class AtoolsActivity extends Activity {
    private ProgressDialog pd;
    private ProgressBar pb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_atools);
        pb = (ProgressBar) findViewById(R.id.progressBar);


    }

    /**
     * 点击事件，进入号码归属地查询的页面
     *
     * @param view
     */
    public void numberQuery(View view) {
        Intent intent = new Intent(this, NumberAddressQueryActivity.class);
        startActivity(intent);

    }

    /**
     * 常用号码查询
     *
     * @param view
     */
    public void commonNumberQuery(View view) {
        startActivity(new Intent(this, CommonNumberActivity.class));
    }


    public void smsBackup(View view) {
//        pd = new ProgressDialog(this);
//        pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//        pd.setMessage("正在备份短信");
//        pd.show();

        new Thread() {
            public void run() {
                try {
                    SmsUtils.backupSms(AtoolsActivity.this, new SmsUtils.BackupCallBack() {
                        @Override
                        public void beforeBackup(int max) {
                            pb.setMax(max);
                        }

                        @Override
                        public void onSmsBackup(int progress) {
                            pb.setProgress(progress);
                        }
                    });
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(AtoolsActivity.this, "SmsBackup", Toast.LENGTH_SHORT).show();
                        }
                    });
                } catch (Exception e) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(AtoolsActivity.this, "SmsBackup failed", Toast.LENGTH_SHORT).show();
                        }
                    });

                    e.printStackTrace();
                } finally {
//                    pd.dismiss();
                }
            }
        }.start();

    }

    public void smsRestore(View view) {
        pd = new ProgressDialog(this);
        pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        pd.setMessage("正在恢复短信");
        pd.show();
        new Thread() {
            public void run() {
                try {
                    SmsUtils.retoreSms(AtoolsActivity.this, true, new SmsUtils.BackupCallBack() {
                        @Override
                        public void beforeBackup(int max) {
                            pd.setMax(max);
                        }

                        @Override
                        public void onSmsBackup(int progress) {
                            pd.setProgress(progress);
                        }
                    });
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(AtoolsActivity.this, "Smsrestore success!", Toast.LENGTH_SHORT).show();
                        }
                    });
                } catch (Exception e) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(AtoolsActivity.this, "Smsrestore failed", Toast.LENGTH_SHORT).show();
                        }
                    });

                    e.printStackTrace();
                } finally {
                    pd.dismiss();
                }
            }
        }.start();
    }

}
